#include "widget.h"
#include "ui_widget.h"
#include <string.h>
#include <QTcpSocket>
#include <QHostAddress>
#include <QMessageBox>
#include <QDate>
#include <QFile>
#include <QLineEdit>
#define CARDSIZE 16

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    socket = new QTcpSocket(this);

    ui->stackedWidget->setCurrentWidget(ui->page_1);
    this->setWindowTitle("e出行");
    ui->lineEdit_pwd->setEchoMode(QLineEdit::Password);
    connect(socket,SIGNAL(connected()),this,SLOT(connectedSlot()));
    connect(&timer,SIGNAL(timeout()),this,SLOT(timerupdateSlot()));
    connect(socket,SIGNAL(readyRead()),this,SLOT(recvSlot()));
    n = 0;
    timer.start(1000);
}



Widget::~Widget()
{
    delete ui;
}

void Widget::on_login_Button_clicked()
{
    QString usr = ui->lineEdit_usr->text();
    QString pwd = ui->lineEdit_pwd->text();
    if(!QString::compare(usr,"张三"))
    {
        if(!QString::compare(pwd,"123456"))
        {
            socket->connectToHost(QHostAddress("192.168.1.200"),8888);

            ui->lineEdit_pwd->clear();
            ui->lineEdit_usr->clear();
        }
        else
        {
            QMessageBox::warning(this,"错误","密码错误！",QMessageBox::Ok);
        }
    }
    else
        QMessageBox::warning(this,"错误","用户不存在！",QMessageBox::Ok);


}

void Widget::connectedSlot()
{
    ui->stackedWidget->setCurrentWidget(ui->page_2);
    QMessageBox::information(this,"登录","登录成功",QMessageBox::Ok);

}

void Widget::timerupdateSlot()
{
    QDateTime time = QDateTime::currentDateTime();
    QString str = time.toString("yyyy-MM-dd hh:mm:ss ddd");
    ui->time_label->setText(str);
    ui->location_label->setText(showtext.mid(n));
    n++;
    if(n == showtext.length())
    {
        n = 0;
    }
}

void Widget::recvSlot()
{
    arr.clear();
    arr = socket->readAll();
    if("GETDB" == mystring)
    {
        mystring.clear();
        mystring = QString(arr);
        ui->text_label->setText(mystring);
    }
    if("GETGPS" == mystring)
    {
        mystring.clear();
        mystring = QString(arr);
        showtext ="            " + mystring;
    }
}
void Widget::on_register_Button_clicked()
{
    if(ui->lineEdit->text().length() != CARDSIZE)
    {
        QMessageBox::warning(this,tr("注册错误"),tr("不支持的卡类型"));
    }
    else
    {
    arr.clear();
    mystring.clear();
    QByteArray arr;
    QString type;
    if(ui->comboBox->currentText() == "普通卡")
    {
        type = "0";
    }
    else
    {
        type = "1";
    }
    QString mystring = ui->lineEdit->text() + "#" + type;
    arr.append(mystring);
    socket->write(arr);
    }
}

void Widget::on_register2_Button_clicked()
{
        arr.clear();
        mystring.clear();
        mystring = "REGISTER";
        arr.append(mystring);
        socket->write(arr,arr.length());
}

void Widget::on_query_Button_clicked()
{
    arr.clear();
    mystring.clear();
    mystring = "GETDB";
    arr.append(mystring);
    socket->write(arr);
}

void Widget::on_download_Button_clicked()
{
    arr.clear();
    mystring.clear();
    mystring = "CLRDB";
    arr.append(mystring);
    socket->write(arr);
    QString str = ui->text_label->text();
    QString filename("C:/Users/XSL/Desktop/111.txt");
    QFile file(filename);
    if( !file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QMessageBox::warning(this,tr("错误"),tr("打开文件失败"));
        return;
    }
    else
    {
        QTextStream in(&file);
        in << str;
        file.close();
        QMessageBox::information(this,"下载","下载成功",QMessageBox::Ok);
    }

}

void Widget::on_logout_Button_clicked()
{
    socket->disconnected();
    ui->stackedWidget->setCurrentWidget(ui->page_1);
}

void Widget::on_location_queryBtuuon_clicked()
{
    arr.clear();
    mystring.clear();
    mystring = "GETGPS";
    arr.append(mystring);
    socket->write(arr);
}


